<?php
/**
 * Created by PhpStorm.
 * User: aron
 * Date: 2019/6/28
 * Time: 4:54 PM
 */

//include_once "../../../Util/DebugUtil/DebugUtil.php";
header("Content-Type:text/html;charset=UTF-8");

class BanchMarkObject
{
    var $label;
    var $totaltime;
    var $avgtime;
    var $list;
}

function banckMark($fileName, $label = "")
{
    $list = array();
    $oldDataStr = file_get_contents($fileName);
    $datas = explode("\n", $oldDataStr);
    $totalTime = 0;
    foreach ($datas as $data) {
        $startPos = strrpos($data, " = ") + 3;
        $timeStr = substr($data, $startPos);
        $time = round(floatval($timeStr), 5);
        $list[] = $time;
        $totalTime += $time;
    }
    $avgTime = round($totalTime / count($datas), 5);

    $obj = new BanchMarkObject();
    $labelToShow = "";
    if (strlen($label) > 0) {
        $labelToShow = $label . " AvgTime = " . $avgTime;
    } else {
        $labelToShow = $fileName . " AvgTime = " . $avgTime;
    }
    $obj->label = $labelToShow;
    $obj->list = $list;
    $obj->avgtime = $avgTime;
    $obj->totaltime = $totalTime;

    return $obj;
}

$results = array();

$filesString = $_REQUEST["files"];
$objsString = $_REQUEST["objs"];
if ($filesString) {
    $files = json_decode($filesString);

    foreach ($files as $fileName) {
        $filePath = "./uploads/" . $fileName;
        $results[] = banckMark($filePath);
    }
} else if ($objsString) {
    $objs = json_decode($objsString);
    foreach ($objs as $obj) {
        $file = $obj->file;
        $filePath = "./uploads/" . $file;
        $label = $obj->label;
        $results[] = banckMark($filePath, $label);
    }
}

$json = json_encode($results);
echo $json;
